package models

import "wikisaver/config"

type AddAble interface {
	Add()
}

type Entity struct {
	Id   string `db:"id"`
	Type string `db:"type"`
}

func (e Entity) Add() {
	_, err := Conn.Exec(`INSERT INTO entity (id, type) VALUES (?, ?)`, e.Id, e.Type)
	if err != nil {
		SaveChan <- e
	}
}

type Label struct {
	Id       int    `db:"id"`
	Eid      string `db:"eid"`
	Language string `db:"language"`
	Value    string `db:"value"`
}

func (l Label) Add() {
	_, err := Conn.Exec(`INSERT INTO label (eid, language, value) VALUES (?, ?, ?)`,  l.Eid, l.Language, l.Value)
	if err != nil {
		SaveChan <- l
	}
}

type Description struct {
	Id       int    `db:"id"`
	Eid      string `db:"eid"`
	Language string `db:"language"`
	Value    string `db:"value"`
}
func (d Description) Add() {
	_, err := Conn.Exec(`INSERT INTO description (eid, language, value) VALUES (?, ?, ?)`,  d.Eid, d.Language, d.Value)
	if err != nil {
		SaveChan <- d
	}
}

type Alias struct {
	Id       int    `db:"id"`
	Eid      string `db:"eid"`
	Language string `db:"language"`
	Value    string `db:"value"`
}
func (a Alias) Add() {
	_, err := Conn.Exec(`INSERT INTO alias (eid, language, value) VALUES (?, ?, ?)`,  a.Eid, a.Language, a.Value)
	if err != nil {
		SaveChan <- a
	}
}

type SiteLink struct {
	Id     int    `db:"id"`
	Eid    string `db:"eid"`
	Site   string `db:"site"`
	Title  string `db:"title"`
	Badges string `db:"badges"`
}

func (s SiteLink) Add() {
	_, err := Conn.Exec(`INSERT INTO sitelink (eid, site, title, badges) VALUES (?, ?, ?, ?)`, s.Eid, s.Site, s.Title, s.Badges)	
	if err != nil {
		SaveChan <- s
	}
}

func create_entity() {
	if config.Env == "dev" {
		Conn.MustExec("drop table if exists sr;")
		Conn.MustExec("drop table if exists reference;")
		Conn.MustExec("drop table if exists qualifier;")
		Conn.MustExec("drop table if exists statement;")
		Conn.MustExec("drop table if exists snak;")
		Conn.MustExec("drop table if exists label;")
		Conn.MustExec("drop table if exists description;")
		Conn.MustExec("drop table if exists alias;")
		Conn.MustExec("drop table if exists sitelink;")
		Conn.MustExec("drop table if exists entity;")
	}
	entity := `CREATE TABLE entity (
		id varchar(10) PRIMARY KEY,
		type varchar(20)
		);`
	label := `CREATE TABLE label (
		id int AUTO_INCREMENT PRIMARY KEY,
		eid varchar(10),
		language varchar(10),
		value text,
		FOREIGN KEY(eid) REFERENCES entity(id)
		);`
	description := `CREATE TABLE description (
		id int AUTO_INCREMENT PRIMARY KEY,
		eid varchar(10),
		language varchar(10),
		value text,
		FOREIGN KEY(eid) REFERENCES entity(id)
		);`
	alias := `CREATE TABLE alias (
		id int AUTO_INCREMENT PRIMARY KEY,
		eid varchar(10),
		language varchar(10),
		value text,
		FOREIGN KEY(eid) REFERENCES entity(id)
		);`
	sitelink := `CREATE TABLE sitelink (
		id int AUTO_INCREMENT PRIMARY KEY,
		eid varchar(10),
		site varchar(255),
		title varchar(255),
		badges text,
		FOREIGN KEY(eid) REFERENCES entity(id)
		)`
	Conn.MustExec(entity)
	Conn.MustExec(label)
	Conn.MustExec(description)
	Conn.MustExec(alias)
	Conn.MustExec(sitelink)
}

func init() {
	create_entity()
}
